package contacts530.application;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class addStudent extends JDialog implements ActionListener {
	private JPanel p1 ,p2;
	private  JLabel name,gender,age,dbo,phone,email,qq;
	private  JTextField s_name,s_age,s_dbo,s_phone,s_email,s_qq;
	private  JButton sure,cancel;
	private  JComboBox t_gender;
	private  JLabel l_dbo,l_email;
  public addStudent(Frame s,String c,Boolean m){    
      super(s,c,m);
      name=new JLabel("姓名");
      name.setBounds(40,50,60,40);
      gender=new JLabel("宿舍名");
      gender.setBounds(40,100,60,40);
      age=new JLabel("年龄");
      age.setBounds(40,150,60,40);
      dbo=new JLabel("生日");
      dbo.setBounds(40,200,60,40);
      phone=new JLabel("手机");
      phone.setBounds(40,250,60,40);
      email=new JLabel("Email");
      email.setBounds(40,300,60,40);
      qq=new JLabel("QQ");
      qq.setBounds(40,350,60,40);
      s_name=new JTextField();
      s_name.setBounds(100,55,100,25);   
      String[] Gender={"泰山420","泰山421","华山420","华山304","启林502","黑山402","泰山503","黑山509"};
      t_gender=new JComboBox(Gender);
      t_gender.setBounds(100,105,100,25);      
      s_age=new JTextField();
      s_age.setBounds(100,155,100,25);
      s_dbo=new JTextField();
      s_dbo.setBounds(100,205,100,25);
      l_dbo = new JLabel("如：2001-09-25");
      l_dbo.setBounds(100,228,125,25);
      s_phone=new JTextField();
      s_phone.setBounds(100,255,100,25);
      s_email=new JTextField();
      s_email.setBounds(100,305,100,25);
      l_email=new JLabel("如:1356@qq.com");
      l_email.setBounds(100,328,150,25);
      s_qq=new JTextField();
      s_qq.setBounds(100,355,100,25);
      sure=new JButton("确定");
      sure.addActionListener(this);
      sure.setBounds(55,450,60,30);
      cancel=new JButton("取消");
      cancel.setBounds(135,450, 60,30);
      cancel.addActionListener(this);     
      this.add(name);
      this.add(gender);
      this.add(age);
      this.add(dbo);
      this.add(phone);
      this.add(email);
      this.add(qq);        
      this.add(s_name);
      this.add(t_gender);
      this.add(s_age);
      this.add(s_dbo);
      this.add(s_phone);
      this.add(s_email);
      this.add(s_qq);      
      this.add(l_dbo);
      this.add(l_email);
      this.add(sure);
      this.add(cancel);
      this.setTitle(c);
      this.setSize(270,600);
      this.setLayout(null);
      this.setVisible(true);
      this.setLocation(500,300);
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
  }
    @Override
    public void actionPerformed(ActionEvent e) {
       String button=e.getActionCommand();
           if(button.equals("取消")){
               this.dispose();
           }
          else if(button.equals("确定"))
          {    
      PreparedStatement ps=null;
      Connection ct=null;
      ResultSet rs=null;
        try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost;database =contactsdb;user=sa;password=zyn416427";
        ct = DriverManager.getConnection(url);
            ps=ct.prepareStatement("insert into student values(?,?,?,?,?,?,?)");
           ps.setString(1,s_name.getText());
           ps.setString(2,t_gender.getSelectedItem().toString());
           ps.setString(3,s_age.getText());
           ps.setString(4,s_dbo.getText());
           ps.setString(5,s_phone.getText());
           ps.setString(6,s_email.getText());
           ps.setString(7,s_qq.getText());
//           ps.setString(8,s_adress.getText());          
           ps.executeUpdate();
           
           this.dispose();
           
        }catch(ClassNotFoundException  e1){
            System.out.println("ERROR:"+e1);
        }catch(SQLException e2Exception){
            JOptionPane.showMessageDialog(this,"格式输入有误");
        }
        finally{
          try{
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(ct!=null){
                    ct.close();
                }
            }
                catch(SQLException e2){
                
            }
        }
   
   }                
    }
    
}
